// service -> user.js
const Service = require('egg').Service
class ArticleService extends Service {

    async createArticle(param){
        console.log(param);
        const data = param ||{authorId:'no0003',title:'happy Day333',content:'Hello World333',isPassed:false,ImgUrl:['https://ts1.cn.mm.bing.net/th/id/R-C.5245459c4835900f30183bebecb3cb55?rik=koS%2bxytGvrBRHw&riu=http%3a%2f%2fpic.zsucai.com%2ffiles%2f2013%2f0723%2fsdidjj4.jpg&ehk=WJLRakwfHBZS2aO2sK%2bCdh4ijkXwyYijy5Z2BFUdnz4%3d&risl=&pid=ImgRaw&r=0','https://img.zmtc.com/2020/1204/20201204084219498.jpg']}
        for(let i = 0;i<5;i++){
            await this.ctx.model.Article.create(data)
        }
        return {
            data:{},
            msg:'创建文章成功'
        }
    }

    async getArticleData() {
    // 从数据库里查询
        const res = await this.ctx.model.Article.find()
        return {
            data:res,
            msg:'请求文章数据成功'
        }
    }

    async passArticle(param){
        const id = param.id
        const data = param.data
        await this.ctx.model.Article.updateMany({"_id":{$in:id}},data)
        const res = await this.ctx.model.Article.find()
        return {
            msg:'审核成功',
            data:res
        }
    }

    async deleteArticle(param){
        const id = param.id
        await this.ctx.model.Article.remove({"_id":{$in:id}})
        const res = await this.ctx.model.Article.find()
        return {
            msg:'删除成功',
            data:res
        }
    }


    async clear(){
        await this.ctx.model.Article.remove()
        return {
            data:{},
            msg:'删除成功'
        }
    }
}

module.exports = ArticleService